package com.wing.cms.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wing.common.util.GUID;
import com.wing.common.util.PageBean;
import com.wing.cms.dao.LayoutDao;
import com.wing.cms.domain.Layout;
import com.wing.sys.domain.Auth;
import com.wing.sys.service.AuthManager;
/**
 * 
 * @author please write yout name
 * 
 */
@Service
@Transactional
public class LayoutManager {
	@Autowired
	private LayoutDao layoutdao;
	@Autowired
	private AuthManager authManager;
	
	
	/**
	 * 获得该对象所有数据的列表
	 * @param siteId 
	 * 
	 * @return list
	 */
	public List<Map<String,Object>> findAll(String siteId){
		return layoutdao.findAll(siteId);	 
	}
	
	public PageBean findPageList(int currentPage, int pageSize,String siteId){
		return layoutdao.findPageList(currentPage,pageSize,siteId);	 
	}
	
	/**
	 * 通过id得到对象
	 *
	 * @param  id
	 * @return pObject
	 */
	public Layout findById(String id){
		return layoutdao.findById(id);	 
	}
	
	/**
	 * 保存对象
	 *
	 * @param  layout
	 */
	public void saveLayout(Layout layout,String siteId){
		if(layout.getId() == null ||  "".equals(layout.getId())){
			layout.setId(new GUID().toString());
			layoutdao.insert(layout);
			Auth auth = new Auth();
			auth.setId(new GUID().toString());
			auth.setType(Auth.TYPE_LAYOUT);
			auth.setRes_id(layout.getId());
			auth.setOwner_id(siteId);
			authManager.insert(auth);
		}else{
			layoutdao.update(layout);
		}
	}

	/**
	 * 编辑对象
	 *
	 * @param  layout
	 */
	public void updateLayout(Layout layout){
		layoutdao.update(layout);
	}
	
	 /**
	 * 删除对象
	 *
	 * @param  chk 对象id数组
	 */
	public void deleteLayouts(String[] chk) {
		for(int i=0;i<chk.length;i++){
			layoutdao.delete(chk[i]);
			authManager.deleteAuthsByTypeResId(Auth.TYPE_LAYOUT, chk[i]);
		}
	}
}



